package com.ruoyi.omchannel.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.omchannel.domain.OmChannel;
import com.ruoyi.omchannel.helper.QRCodeGenerator;
import com.ruoyi.omchannel.service.IOmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 渠道管理Controller
 * 
 * @author ningyi
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/mp/om/channel")
public class MpOmChannelController extends BaseController
{
    //生成 二维码 https://blog.csdn.net/hk000001/article/details/143452271

    //https://blog.csdn.net/qq_38258642/article/details/128961951


    @Autowired
    private IOmChannelService omChannelService;

    @Autowired
    private QRCodeGenerator qrCodeGenerator;


    /**
     * 查询渠道管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(OmChannel omChannel)
    {
        startPage();
        List<OmChannel> list = omChannelService.selectOmChannelList(omChannel);

        // 遍历渠道列表
        for (OmChannel channel : list) {
            // 使用工具类根据channelUuid生成二维码的base64字符串
            //String base64QRCode = qrCodeGenerator.generateQRCodeAsBase64(channel.getChannelUuid());
            // 文件
            String pathFile = qrCodeGenerator.generateQRCodeToFile("cm_"+channel.getChannelUuid());

            // 将生成的base64二维码字符串设置到当前渠道对象的channelImage属性中
            String  channelPath = RuoYiConfig.getUploadPath()+"/channel/";
            String pathFIle2 = "/profile/upload/jie/"+"cm_"+channel.getChannelUuid()+".png";

            channel.setChannelImage(pathFIle2);

            // 如果需要更新数据库中的记录，请在此处调用相应的更新方法
            // omChannelService.updateOmChannel(channel);
        }

        // 这里可以添加其他逻辑，比如打印处理结果等
        for(OmChannel channel : list) {
            //System.out.println("Channel UUID: " + channel.getChannelUuid() + ", Image Base64: " + channel.getChannelImage());
            System.out.println("Channel UUID: " + channel.getChannelUuid() + ", pathFIle2: " + channel.getChannelImage());
        }

        return getDataTable(list);
    }

    /**
     * 导出渠道管理列表
     */
    @PreAuthorize("@ss.hasPermi('om:channel:export')")
    @Log(title = "渠道管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmChannel omChannel)
    {
        List<OmChannel> list = omChannelService.selectOmChannelList(omChannel);
        ExcelUtil<OmChannel> util = new ExcelUtil<OmChannel>(OmChannel.class);
        util.exportExcel(response, list, "渠道管理数据");
    }

    /**
     * 获取渠道管理详细信息
     */
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody HashMap<String,Object> in)
    {
        String uuid = in.get("uuid").toString();
        return success(omChannelService.getInfo(uuid));
    }

}
